<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>贷款申请列表</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }

        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            background-color: white;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            overflow: hidden;
        }

        th {
            background-color: #4CAF50;
            color: white;
            padding: 15px;
            text-align: left;
            font-weight: bold;
        }

        td {
            padding: 12px 15px;
            border-bottom: 1px solid #ddd;
        }

        tr:hover {
            background-color: #f8f9fa;
        }

        tr:nth-child(even) {
            background-color: #f9f9f9;
        }

        .status-pending {
            background-color: #ffeb3b;
            color: #333;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: bold;
        }

        .status-approved {
            background-color: #4CAF50;
            color: white;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: bold;
        }

        .status-rejected {
            background-color: #f44336;
            color: white;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: bold;
        }

        .action-link {
            display: inline-block;
            margin: 2px;
            padding: 8px 16px;
            text-decoration: none;
            border-radius: 4px;
            font-size: 12px;
            transition: background-color 0.3s;
            text-align: center;
            min-width: 60px;
            box-sizing: border-box;
        }

        .approve-link {
            background-color: #4CAF50;
            color: white;
        }

        .approve-link:hover {
            background-color: #45a049;
        }

        .reject-link {
            background-color: #f44336;
            color: white;
        }

        .reject-link:hover {
            background-color: #da190b;
        }

        .withdraw-link {
            background-color: #2196F3;
            color: white;
        }

        .withdraw-link:hover {
            background-color: #0b7dda;
        }

        .no-image {
            color: #999;
            font-style: italic;
        }

        .loan-image {
            max-width: 100px;
            max-height: 100px;
            border-radius: 4px;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
            cursor: pointer;
            transition: transform 0.2s;
        }

        .loan-image:hover {
            transform: scale(1.05);
        }

        .action-cell {
            white-space: nowrap;
        }

        /* 确保按钮容器居中显示 */
        .action-buttons {
            display: flex;
            gap: 5px;
            justify-content: center;
            align-items: center;
        }

        /* 图片放大模态框样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.8);
            animation: fadeIn 0.3s;
        }

        .modal-content {
            position: relative;
            margin: auto;
            display: block;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            max-width: 90%;
            max-height: 90%;
            border-radius: 8px;
        }

        .modal-content {
            animation: zoomIn 0.3s;
        }

        .close {
            position: absolute;
            top: 20px;
            right: 35px;
            color: #f1f1f1;
            font-size: 40px;
            font-weight: bold;
            cursor: pointer;
            z-index: 1001;
            background-color: rgba(0, 0, 0, 0.5);
            width: 50px;
            height: 50px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: background-color 0.3s;
        }

        .close:hover {
            background-color: rgba(0, 0, 0, 0.8);
        }

        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }

        @keyframes zoomIn {
            from { transform: translate(-50%, -50%) scale(0.8); opacity: 0; }
            to { transform: translate(-50%, -50%) scale(1); opacity: 1; }
        }

        .image-cell {
            text-align: center;
        }
    </style>
</head>
<body>
    <h1>贷款申请列表</h1>

    <table>
        <thead>
            <tr>
                <th>编号</th>
                <th>用户名</th>
                <th>金额</th>
                <th>期限</th>
                <th>图片</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <tr th:each="loan : ${loans}">
                <td th:text="${loan.id}"></td>
                <td th:text="${loan.userName}"></td>
                <td th:text="${loan.price}"></td>
                <td th:text="${loan.term}"></td>
                <td class="image-cell">
                    <img th:if="${loan.img != null}"
                         th:src="@{${loan.img}}"
                         alt="贷款图片"
                         class="loan-image"
                         th:attr="data-src=@{${loan.img}}">
                    <span th:unless="${loan.img != null}" class="no-image">无图片</span>
                </td>
                <td>
                    <span th:if="${loan.status == 0}" class="status-pending">待审核</span>
                    <span th:if="${loan.status == 1}" class="status-approved">已通过</span>
                    <span th:if="${loan.status == 2}" class="status-rejected">已拒绝</span>
                </td>
                <td class="action-cell">
                    <div th:if="${loan.status == 0}" class="action-buttons">
                        <a th:href="@{/loan/updateStatus(id=${loan.id}, status=1)}"
                           class="action-link approve-link">通过</a>
                        <a th:href="@{/loan/updateStatus(id=${loan.id}, status=2)}"
                           class="action-link reject-link">拒绝</a>
                    </div>
                    <div th:if="${loan.status == 1}" class="action-buttons">
                        <a th:href="@{/loan/updateStatus(id=${loan.id}, status=0)}"
                           class="action-link withdraw-link">撤回</a>
                    </div>
                    <div th:if="${loan.status == 2}" style="text-align: center" class="no-image">
                        无操作
                    </div>
                </td>
            </tr>

            <!-- 如果没有数据，显示提示信息 -->
            <tr th:if="${loans == null or #lists.isEmpty(loans)}">
                <td colspan="7" style="text-align: center; padding: 30px; color: #666;">
                    暂无贷款申请数据
                </td>
            </tr>
        </tbody>
    </table>

    <!-- 图片放大模态框 -->
    <div id="imageModal" class="modal">
        <span class="close">&times;</span>
        <img class="modal-content" id="modalImage">
    </div>

    <script>
        // 获取模态框元素
        const modal = document.getElementById("imageModal");
        const modalImg = document.getElementById("modalImage");
        const closeBtn = document.getElementsByClassName("close")[0];

        // 为所有可点击的图片添加事件监听器
        const images = document.querySelectorAll(".loan-image");
        images.forEach(function(img) {
            img.addEventListener("click", function() {
                modal.style.display = "block";
                modalImg.src = this.getAttribute("data-src") || this.src;
            });
        });

        // 点击关闭按钮关闭模态框
        closeBtn.onclick = function() {
            modal.style.display = "none";
        };

        // 点击模态框背景关闭模态框
        modal.onclick = function(event) {
            if (event.target === modal) {
                modal.style.display = "none";
            }
        };

        // 按 ESC 键关闭模态框
        document.addEventListener("keydown", function(event) {
            if (event.key === "Escape" && modal.style.display === "block") {
                modal.style.display = "none";
            }
        });
    </script>
</body>
</html>
